﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseAccess;
using System.Data;

namespace Liguo.Administration
{
    public partial class UnfinishedOrders : System.Web.UI.Page
    {        
        private DataSet ds = new DataSet();
        private DataSet dsmod = new DataSet();
        private SqlIt sql = new SqlIt();

        private DateTime currenttime=new DateTime();                //当前时间
        private static DateTime lasttime=new DateTime();                  //上一次送货时间
        public static ArrayList orderids=new ArrayList();               //传递到SendGoods.aspx的orderid数组
        public static ArrayList delivery = new ArrayList();
        private const int maxTimeSpan = 10;                     //送货时间间隔
        private const int maxOrderNum=5;                       //警报订单数
        private static bool xiang;
        protected void Page_Load(object sender, EventArgs e)
        {
           
            currenttime = DateTime.Now;
            ds = sql.SpecialItem("OrderInfo", "Status=1");
            if (ds.Tables[0].Rows.Count <= 0){
                Response.Write("<script>alert('暂时没有新到货~~~')</script>");
                xiang = true;
                return;
            }

            if (!IsPostBack)
            {
                currenttime = lasttime = DateTime.Now;
                bind(ds);
                xiang = true;
            }

            if ((
                (ds.Tables[0].Rows.Count > maxOrderNum)
                ||
                ((currenttime.Minute - lasttime.Minute >= maxTimeSpan) && ds.Tables[0].Rows.Count > 0)             
               ) && xiang )
            {
                    Response.Write(
                    "<body><object data=\"../res/horse.mp3\" style=\"height:0px;width:0px\"></object></body>"
                     );
                    Response.Write("<script>alert('送货喽~~~')</script>");
                    lasttime = currenttime;
                    if ((ds.Tables[0].Rows.Count > maxOrderNum)
                        && (currenttime.Minute - lasttime.Minute < maxTimeSpan))
                    {
                        xiang = false;
                    }
                    else
                    {
                        xiang = true;
                    }
            }
                        
        }

        private void bind(DataSet ds)
        {
            GridView1.DataSource = ds;
            GridView1.DataKeyNames = new String[] { "ID" };
            GridView1.DataBind();
            DropDownList ddl;
                        
            //UnfinishedOrders中droplist 0:待完成 1:完成订单 2:删除订单(退订) 3:用户已拒收
            //数据库中OrderStatus表
            //0：其他  1：未完成   2：完成    3：拒收    4：退订

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)         
            {
                DataTable dt = new DataTable();
                DataRow drv = dt.NewRow();
                drv = ds.Tables[0].Rows[i];
                int a=Convert.ToInt32(drv["Status"]);
                switch (a)
                {
                    case 1:
                    {
                        ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
                        ddl.Items.FindByValue("0").Selected = true;
                        break;
                    }
                    case 2:
                    {
                        ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
                        ddl.Items.FindByValue("1").Selected = true;
                        break;
                    }
                    case 3:
                    {
                        ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
                        ddl.Items.FindByValue("3").Selected = true;
                        break;
                    }
                    case 4:
                    {
                        ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
                        ddl.Items.FindByValue("2").Selected = true;
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList DropDownList1 = sender as DropDownList;
            int index = (DropDownList1.NamingContainer as GridViewRow).RowIndex;//命名容器
            String id = GridView1.DataKeys[index].Value.ToString();//设定的datakey属性
            String selectedv = DropDownList1.SelectedValue;
            String deliv = Convert.ToString(sql.SingleSpecialItem("OrderInfo", "ID=" + id, "Delivery"));

            Dictionary<string, string> ud = new Dictionary<string, string>();
            String constraint;
            if (ds.Tables[0].Rows.Count <= 0)
                return;
            if (selectedv.Equals("1")){
                //Response.Write("<script>confirm('确认要完成？')</script>");
                ds.Tables[0].Rows[index]["Status"] = selectedv;
                ud["Status"] = "2";
                constraint= "ID='" + id + "'";
                orderids.Add(id);
                delivery.Add(deliv);
                sql.UpdateMess("OrderInfo", ud, constraint);
            }
            else{
                //Response.Write("<script>confirm('确认要删除(退订)？')</script>");
                ds.Tables[0].Rows[index]["Status"] = selectedv;     
                ud["Status"] = "4";
                constraint = "ID='" + id + "'";
                sql.UpdateMess("OrderInfo", ud, constraint);
            }
                        
            Session["SendGoodsID"] = orderids;
            Session["SendGoodsDel"] = delivery;
            ds = sql.SpecialItem("OrderInfo", "Status=1");
            bind(ds);

        }

        //向车屌OrderInquiry.aspx界面传递TextSearchUserName
        protected void ButtonModifyOrders_Clicked(object sender, EventArgs e)
        {
            if (TextSearchUserName.Text.Equals(""))
            {
                Response.Write("<script>alert('请输入要求修改订单的用户名')</script>");
            }
            else{
                Response.Redirect("/Administration/OrderInquiry.aspx?UserName=" + TextSearchUserName.Text);
            }
        }
    }
}